﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GD.ModelContest.Common;
using GD.ModelContest.Model;
using GD.ModelContest.Service;

namespace GD.ModelContest.Web
{
    public partial class UserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind_User(0,string.Empty,string.Empty,string.Empty);
            }
        }
        protected void DataBind_User(int recordcount,string name,string items, string sex)
        {
            if (recordcount == 0)
            {
                this.AspNetPager_User.CurrentPageIndex = 1;
            }
            int count = recordcount;
            int pageIndex = this.AspNetPager_User.CurrentPageIndex;
            int pageSize = this.AspNetPager_User.PageSize;
            this.dlUserList.DataSource = ServiceLocator.IUserService.GetUserCollection(name, sex, items, string.Empty, string.Empty, string.Empty, string.Empty, pageIndex, pageSize, ref count);
            this.dlUserList.DataBind();
            this.AspNetPager_User.RecordCount = count;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string name = this.txtName.Text == null ? string.Empty : this.txtName.Text.Trim();
            string sex = this.rblSex.SelectedValue == null ? string.Empty : this.rblSex.SelectedValue.ToString();
            string items = this.ddlGroup.SelectedValue == "0" ? string.Empty : this.ddlGroup.SelectedValue.ToString();
            DataBind_User(0, name, sex, items);
        }

        protected void dlUserList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                mc_user mcUser = (mc_user)e.Item.DataItem;
                

                Image imgHeadPhoto = (Image)e.Item.FindControl("imgHeadPhoto");
                imgHeadPhoto.ImageUrl = "/ImgUpload/" + GetImgUrlByID(mcUser.userId);
                HyperLink hlName = (HyperLink)e.Item.FindControl("hlName");
                hlName.Text = mcUser.name;
                hlName.NavigateUrl = string.Format("UserView.aspx?UserId={0}", mcUser.userId);
                Label lbPinCode = (Label)e.Item.FindControl("lbPinCode");
                lbPinCode.Text = mcUser.pinCode;
                Label lbVoteNum = (Label)e.Item.FindControl("lbVoteNum");
                lbVoteNum.Text = GetVoteNumByID(mcUser.userId);
            }
        }

        protected string GetImgUrlByID(int userId)
        {
            mc_photo mcPhoto = ServiceLocator.IUserService.GetHeadPhotoByUserId(userId);
            if (mcPhoto != null)
            {
                return mcPhoto.photoName;
            }
            else
            {
                return "";
            }
        }

        protected string GetVoteNumByID(int userId)
        {
            mc_vote mcVote = ServiceLocator.IUserService.GetVoteByUserId(userId);
            if (mcVote != null)
            {
                return mcVote.voteNum.ToString();
            }
            else
            {
                return "";
            }
        }

        protected void AspNetPager_User_PageChanged(object sender, EventArgs e)
        {
            string name = this.txtName.Text == null ? string.Empty : this.txtName.Text.Trim();
            string sex = this.rblSex.SelectedValue == null ? string.Empty : this.rblSex.SelectedValue.ToString();
            string items = this.ddlGroup.SelectedValue == "0" ? string.Empty : this.ddlGroup.SelectedValue.ToString();
            DataBind_User(0, name, sex, items);
        }
    }
}
